package com.rdgk.newtech.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.rdgk.newtech.dao.DataDicDao;
import com.rdgk.newtech.service.DataDicService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author: msun
 */
@Service
public class DataDicServiceImpl implements DataDicService {
    @Autowired
    private DataDicDao dataDicDao;

    /**
     * 显示数据字典类型列表
     */
    @Override
    public com.alibaba.fastjson.JSONObject listDataDicType() {
        List<JSONObject> list = dataDicDao.listDataDicType();
        com.alibaba.fastjson.JSONObject result = CommonUtil.successJson();
        com.alibaba.fastjson.JSONObject info = new com.alibaba.fastjson.JSONObject();
        info.put("list", getTreeJsonByTable(list));
        result.put("info", info);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject addDataDicType(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.addDataDicType(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject updateDataDicType(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.updateDataDicType(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject deleteDataDicType(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.deleteDataDicType(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    public com.alibaba.fastjson.JSONObject listDataDic(com.alibaba.fastjson.JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int finalTid = jsonObject.getIntValue("finalTid");
        List<com.alibaba.fastjson.JSONObject> list = null;
        int count = 0;
        if (finalTid == -1){
            list = dataDicDao.listAllDataDic(jsonObject);
            count = dataDicDao.countDataDic(jsonObject);
        }else {
            list = dataDicDao.listDataDicByType(jsonObject);
            count = dataDicDao.countDataDicByType(jsonObject);
        }
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    public com.alibaba.fastjson.JSONObject listDataDicBySearch(com.alibaba.fastjson.JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int finalTid = jsonObject.getIntValue("finalTid");
        List<com.alibaba.fastjson.JSONObject> list = null;
        int count = 0;
        if (finalTid == -1){
            list = dataDicDao.listDataDicBySearch(jsonObject);
            count = dataDicDao.countDataDicBySearch(jsonObject);
        }else {
            list = dataDicDao.listDataDicBySearchAndType(jsonObject);
            count = dataDicDao.countDataDicBySearchAndType(jsonObject);
        }
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject addDataDic(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.addDataDic(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject updateDataDic(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.updateDataDic(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public com.alibaba.fastjson.JSONObject deleteDataDic(com.alibaba.fastjson.JSONObject jsonObject) {
        dataDicDao.deleteDataDic(jsonObject);
        return CommonUtil.successJson();
    }

    private JSONArray getTreeJsonByTable(List<JSONObject> list){
        JSONArray typeResultList = new JSONArray();
        while(list.size() != 0){
            Iterator<JSONObject> iterator = list.iterator();
            while (iterator.hasNext()){
                JSONObject row = iterator.next();
                if (row.getInt("pid") == 0){
                    row.put("children",new JSONArray[]{});
                    typeResultList.add(row);
                    iterator.remove();
                }else{
                    for (Object o: typeResultList) {
                        if (getJsonArrayByPid(row.getInt("pid"), (JSONObject)o, row)){
                            iterator.remove();
                            break;
                        }
                    }
                }

            }
            list = IteratorUtils.toList(iterator);
        }
        return typeResultList;
    }

    private Boolean getJsonArrayByPid(int pid, JSONObject parent, JSONObject child){
        System.out.println("in--parent+++++" + parent.toString());
        boolean flag = false;
        if ( pid != parent.getInt("id")){
            for (Object oo: parent.getJSONArray("children")) {
                getJsonArrayByPid(pid, (JSONObject) oo, child);
            }
        }else {
            child.put("children",new JSONArray[]{});
            parent.getJSONArray("children").add(child);
            flag = true;
        }
        return flag;
    }
}
